package com.zhengke.controller.cggl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhengke.common.object.AjaxDone;
import com.zhengke.common.object.Result;
import com.zhengke.pojo.TvPurchaseDetails;
import com.zhengke.pojo.TvPurchaseTemporary;
import com.zhengke.service.cggl.CGDHService;

/**
 * *******************************
 *   	项目名称：tvmall-parent	 *
 * 		类说明：  采购到货			 * 
 * 		@author 周梦坤			 *
 * 		@version 1.0			 *	
 * *******************************
 */
@Controller
@RequestMapping("CGDH")
public class CGDHController {
	@Autowired
	CGDHService service;
	/**
	 * 	跳转到采购到货页面
	 * @return
	 */
	@RequestMapping("toCGDH.do")
	public String toCGDHList() {
		return "cggl/list";
	}
	/**
	 * 查询到货
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryList.do")
	public Result queryList(String goodsName,int page,int limit) {
		TvPurchaseDetails tvp = new TvPurchaseDetails();
		tvp.setGoodsName(goodsName);
		return service.queryList(tvp, page, limit);
	}
	/**
	 * 确认实际到货数量
	 */
	@RequestMapping("toConfirm.do")
	public String toConfirm(HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		String purchaseId=request.getParameter("purchaseId");
		String purchaseGoodsId =request.getParameter("purchaseGoodsId");
		TvPurchaseDetails tvp = new TvPurchaseDetails();
		tvp.setPurchaseId(purchaseId);
		tvp.setPurchaseGoodsId(Integer.valueOf(purchaseGoodsId));
		List<TvPurchaseTemporary> tvptList = service.queryType(tvp);
		model.put("tvptList", tvptList);
		return "cggl/confirm2";
	}
	/**
	 * 	修改实际到货数量
	 * @param tvp
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("updTvp.do")
	@ResponseBody
	public AjaxDone updTvp(Long[] realNum,Long[] goodsId, int purchaseGoodsId,int servicePointId,int purchaseId) {
		TvPurchaseDetails tvp = new TvPurchaseDetails();
		tvp.setPurchaseGoodsId(purchaseGoodsId);
		tvp.setPurchaseId(String.valueOf(purchaseId));
		System.err.println(purchaseId+"---------------------");
		tvp.setTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		tvp.setServicePointId(servicePointId);
		if(realNum.length==1) {
			tvp.setRealNum(realNum[0].intValue());
			tvp.setGoodsId(goodsId[0].intValue());
			service.updTvp(tvp);
			service.updSTock(tvp);
			return new AjaxDone("200", "确认到货成功");
		}else {
			int temp=0;
			for(int i=0;i<realNum.length;i++) {
				temp+=realNum[i];
				tvp.setRealNum(realNum[i].intValue());
				tvp.setGoodsId(goodsId[i].intValue());
				service.updSTock(tvp);
				
			}
			tvp.setRealNum(temp);
			service.updTvp(tvp);
			return new AjaxDone("200", "确认到货成功");
		}
	}
	
}
